package cn.tedu.musicsys.service.impl;

import cn.tedu.musicsys.domain.DownloadRecord;
import cn.tedu.musicsys.domain.User;
import cn.tedu.musicsys.mapper.RecordDownloadMapper;
import cn.tedu.musicsys.mapper.UserMapper;
import cn.tedu.musicsys.service.RecordDownloadService;
import cn.tedu.musicsys.utils.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Service("recordDownloadService")
public class RecordDownloadServiceImpl implements RecordDownloadService {
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private RecordDownloadMapper recordDownloadMapper;
	@Override
	public void recordDownload(HttpServletRequest request, int songId) {
		User user= Request.getUserFromHttpServletRequest(request);
		if(user==null) {
			return;
		}
		//记录当前用户的下载记录
		user=userMapper.selectByUser(user);
		recordDownloadMapper.insert(new DownloadRecord(user.getUserId(),songId));
	}
	@Override
	public List<DownloadRecord> getAllRecords() {
		return recordDownloadMapper.selectAll();
	}

}
